package com.qqtech.qquan.circle.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.circle.model.CircleTopic;
import com.qqtech.qquan.circle.model.CircleTopicQuery;
import com.qqtech.qquan.circle.model.CircleTopicVo;

import jersey.repackaged.com.google.common.collect.Lists;

@Repository
public class CircleTopicDaoImpl extends BaseDaoImpl<CircleTopic> implements CircleTopicDao {
	public static final Logger log = LoggerFactory.getLogger(CircleTopicDaoImpl.class);

	@Override
	public int updateByCircleId(CircleTopic topic) {
		if(topic==null){
			return 0;
		}
		if(topic.getCircleId()==null){
			return 0;
		}
		return sqlSessionTemplate.update(getSqlName("updateByCircleId"), topic);
	}

	@Override
	public int increasePv(int topicId, int pv) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setPv(pv);
		return sqlSessionTemplate.update(getSqlName("increasePv"), topic);
	}
	
	@Override
	public Page<CircleTopicVo> queryPageListAdmin(CircleTopicQuery query, Pageable pageable) {
		List<CircleTopicVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
		int count = sqlSessionTemplate.selectOne(getSqlName("selectCountAdmin"), BeanUtil.toMap(query));
		return new PageImpl<CircleTopicVo>(contentList, pageable, count);
	}
	
	@Override
	public List<CircleTopicVo> queryList4Admin(CircleTopicQuery query, Pageable pageable) {
		return sqlSessionTemplate.selectList(getSqlName("select4Admin"), getParams(query, pageable));
	}
	
	@Override
	public int queryCountAdmin(CircleTopicQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountAdmin"), BeanUtil.toMap(query));
	}
	
	@Override
	public List<Integer> queryCreatorIds(Integer circleId) {
		return sqlSessionTemplate.selectList(getSqlName("queryCreatorIds"), circleId);
	}

	@Override
	public int increaseComment(Integer topicId, int addCount) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setCommentCount(addCount);
		return sqlSessionTemplate.update(getSqlName("increaseComment"), topic);
	}

	@Override
	public int increasePraise(Integer topicId, int addCount) {
		CircleTopic topic = new CircleTopic();
		topic.setId(topicId);
		topic.setPraiseCount(addCount);
		return sqlSessionTemplate.update(getSqlName("increasePraise"), topic);
	}

	@Override
	public List<Integer> queryMyTopicCircleIds(Integer userId) {
		List<Integer> circleListDistinct = Lists.newArrayList();
		List<Integer> circleList = sqlSessionTemplate.selectList(getSqlName("queryMyTopicCircleIds"), userId);
		if(circleList!=null && circleList.size()>0){
			for(Integer circleId : circleList){
				if(!circleListDistinct.contains(circleId)){
					circleListDistinct.add(circleId);
				}
			}
		}
		return circleListDistinct;
	}
}
